import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import Detail from './Detail'
import { Route } from 'react-router-dom'


export default class Message extends Component {
  state = {
    messageArr: [
      {id: '001', title: '消息1'},
      {id: '002', title: '消息2'},
      {id: '003', title: '消息3'},
    ]
  }

  replaceShow = (id, title) => {
    // replace跳转 + 携带params参数
    // this.props.history.replace(`/home/message/detail/${id}/${title}`)

    // replace跳转 + 携带Search参数
    // this.props.history.replace(`/home/message/detail/?id=${id}&title=${title}`)

    // replace跳转 + 携带state参数
    this.props.history.replace(`/home/message/detail`, {id, title})
  }

  pushShow = (id, title) => {
    // push跳转 + 携带params参数
    // this.props.history.push(`/home/message/detail/${id}/${title}`)

    // push跳转 + 携带Search参数
    // this.props.history.push(`/home/message/detail/?id=${id}&title=${title}`)
    // push跳转 + 携带Search参数
    this.props.history.push(`/home/message/detail/`, {id, title})
  }

  goBack = () => {
    this.props.history.goBack()
  }

  goForward = () => {
    this.props.history.goForward()
  }

  go=() => {
    // this.props.history.go(n) // 你是负数是 就回退， 是正数时前进
  }
  
  
  
  
  
  render() {
    const {messageArr} = this.state
    return (
      <div>
        <ul>
          {
            messageArr.map((message)=>{
              return (
                <li key={message.id}>
                  {/* 向路由组件传递params参数 */}
                  {/* <Link to={`/home/message/detail/${message.id}/${message.title}`} >{message.title}</Link>  */}
                  {/* 向路由组件传递search参数 */}
                  {/* <Link to={`/home/message/detail/?id=${message.id}&title=${message.title}`} >{message.title}</Link> */}
                  {/* 向路由组件传递state参数 */}
                  {/* <Link to={{pathname: '/home/message/detail', state:{id:message.id, title:message.title}}} >{message.title}</Link> */}
                 <Link to={{pathname: '/home/message/detail', state:{id:message.id, title:message.title}}} >{message.title}</Link>
                  &nbsp; <button onClick={()=>this.pushShow(message.id, message.title)}>push查看</button>
                  &nbsp; <button onClick={()=>this.replaceShow(message.id, message.title)}>replace查看</button>
                </li>
              )
            })
          }
        </ul>

        <hr />
        {/* 第一种携带参数的方式 生命接收Params参数 */}
        {/* <Route path="/home/message/detail/:id/:title" component={Detail} /> */}
        {/* 第一种携带参数的方式 声明接收Seacrch参数 无需声明接收，正常声明即可*/}
        {/* <Route path="/home/message/detail" component={Detail} /> */}

        {/* 第一种携带参数的方式 声明接收State参数 无需声明接收，正常声明即可*/}
        <Route path="/home/message/detail" component={Detail} />


        <button onClick={this.goBack}>回退</button>
        <button onClick={this.goForward}>前进</button>

      </div>
    )
  }
}
